<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <div>
        <el-form-item label="付款单编号">
          <el-input v-model="queryParams.payOrderNo" placeholder="" clearable></el-input>
        </el-form-item>
        <el-form-item label="业主姓名">
          <el-input v-model="queryParams.ownerName" placeholder="" clearable></el-input>
        </el-form-item>
      </div>
      <div>
        <el-form-item label="付款状态">
          <el-select v-model="queryParams.payState" placeholder="请选择" clearable>
            <el-option v-for="item in payStateList" :label="item.dictLabel" :value="item.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账单类型">
          <el-select v-model="queryParams.billType" placeholder="请选择" clearable>
            <el-option v-for="item in billTypeList" :label="item.dictLabel" :value="item.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收房合同编号">
          <el-input v-model="queryParams.sfContractNo" placeholder="" clearable></el-input>
        </el-form-item>
      </div>
      <div>
        <el-form-item label="付款日期">
          <el-date-picker
            clearable
            v-model="queryParams.startDate"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <span>至</span>
        <el-form-item label="">
          <el-date-picker
            clearable
            v-model="queryParams.endDate"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
      </div>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-tabs v-model="activeName" @tab-click="handleClick">

      <el-tab-pane label="收房付款单" name="first">
        <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="合同编号" align="center" prop="sfContractNo" />
          <el-table-column label="房产信息" align="center" prop="houseName"/>
          <el-table-column label="业主姓名" align="center" prop="ownerName" />
          <el-table-column label="支付金额" align="center" prop="paymentAmount" />
          <el-table-column label="账期" align="center" prop="payNumber">
            <template slot-scope="scope">
              第{{scope.row.payNumber}}期
            </template>
          </el-table-column>
          <el-table-column label="付款状态" align="center" prop="payState">
            <template slot-scope="scope">
              <span v-for="item in payStateList" v-if="scope.row.payState==item.dictValue">{{item.dictLabel}}</span>
            </template>
          </el-table-column>
          <el-table-column label="账单类型" align="center" prop="billType">
            <template slot-scope="scope">
              <span v-for="item in billTypeList" v-if="scope.row.billType==item.dictValue">{{item.dictLabel}}</span>
            </template>
          </el-table-column>
          <el-table-column label="应付日期" align="center" prop="payableDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.payableDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="付款日期" align="center" prop="paidTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.paidTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="支付方式" align="center" prop="payMethod">
            <template slot-scope="scope">
              <span v-for="item in payMethodList" v-if="scope.row.payMethod==item.dictValue">{{item.dictLabel}}</span>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <span v-if="scope.row.payState==1">
                 <el-button
                   size="mini"
                   type="text"
                   icon="el-icon-edit"
                   @click="choosePayMethod(scope.row)"
                   v-hasPermi="['system:plan:edit']"
                 >支付</el-button>
              </span>
              <span v-if="scope.row.payState!=1">
                --
              </span>

            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>


      <el-tab-pane label="租务付款单" name="second">
        <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="合同编号" align="center" prop="sfContractNo" />
          <el-table-column label="房产信息" align="center" prop="houseName"/>
          <el-table-column label="业主姓名" align="center" prop="ownerName" />
          <el-table-column label="支付金额" align="center" prop="paymentAmount" />
          <el-table-column label="账期" align="center" prop="payNumber">
            <template slot-scope="scope">
              第{{scope.row.payNumber}}期
            </template>
          </el-table-column>
          <el-table-column label="付款状态" align="center" prop="payState">
            <template slot-scope="scope">
              <span v-for="item in payStateList" v-if="scope.row.payState==item.dictValue">{{item.dictLabel}}</span>
            </template>
          </el-table-column>
          <el-table-column label="账单类型" align="center" prop="billType">
            <template slot-scope="scope">
              <span v-for="item in billTypeList" v-if="scope.row.billType==item.dictValue">{{item.dictLabel}}</span>
            </template>
          </el-table-column>
          <el-table-column label="应付日期" align="center" prop="payableDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.payableDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="付款日期" align="center" prop="paidTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.paidTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="支付方式" align="center" prop="payMethod">
            <template slot-scope="scope">
              <span v-for="item in payMethodList" v-if="scope.row.payMethod==item.dictValue">{{item.dictLabel}}</span>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <span v-if="scope.row.payState==1">
                 <el-button
                   size="mini"
                   type="text"
                   icon="el-icon-edit"
                   @click="choosePayMethod(scope.row)"
                   v-hasPermi="['system:plan:edit']"
                 >支付</el-button>
              </span>
              <span v-if="scope.row.payState!=1">
                --
              </span>

            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>

      <el-tab-pane label="金融付款单" name="third">
        <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="合同编号" align="center" prop="sfContractNo" />
          <el-table-column label="房产信息" align="center" prop="houseName"/>
          <el-table-column label="业主姓名" align="center" prop="ownerName" />
          <el-table-column label="支付金额" align="center" prop="paymentAmount" />
          <el-table-column label="账期" align="center" prop="payNumber">
            <template slot-scope="scope">
              第{{scope.row.payNumber}}期
            </template>
          </el-table-column>
          <el-table-column label="付款状态" align="center" prop="payState">
            <template slot-scope="scope">
              <span v-for="item in payStateList" v-if="scope.row.payState==item.dictValue">{{item.dictLabel}}</span>
            </template>
          </el-table-column>
          <el-table-column label="账单类型" align="center" prop="billType">
            <template slot-scope="scope">
              <span v-for="item in billTypeList" v-if="scope.row.billType==item.dictValue">{{item.dictLabel}}</span>
            </template>
          </el-table-column>
          <el-table-column label="应付日期" align="center" prop="payableDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.payableDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="付款日期" align="center" prop="paidTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.paidTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="支付方式" align="center" prop="payMethod">
            <template slot-scope="scope">
              <span v-for="item in payMethodList" v-if="scope.row.payMethod==item.dictValue">{{item.dictLabel}}</span>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <span v-if="scope.row.payState==1">
                 <el-button
                   size="mini"
                   type="text"
                   icon="el-icon-edit"
                   @click="choosePayMethod(scope.row)"
                   v-hasPermi="['system:plan:edit']"
                 >支付</el-button>
              </span>
              <span v-if="scope.row.payState!=1">
                --
              </span>

            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <el-dialog title="付款" :visible.sync="dialogFormVisible">
      <el-form v-model="payForm">
        <el-form-item label="付款金额" :label-width="formLabelWidth">
          {{payForm.paymentAmount}}
        </el-form-item>
        <el-form-item label="支付方式" :label-width="formLabelWidth">
          <el-select v-model="payForm.payMethod" placeholder="请选择">
            <el-option v-for="item in payMethodList" :label="item.dictLabel" :value="item.dictValue"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="payMoney">确 定</el-button>
      </div>
    </el-dialog>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listPlan, getPlan, delPlan, addPlan, updatePlan,dictInfo,payInfo } from "@/api/finance/paymentplan";
import Termination from "../../shoufang/termination/index";

export default {
  name: "Paymentplan",
  components: {
    Termination
  },
  data() {
    return {
      formLabelWidth: '120px',
      payForm: {},
      dialogFormVisible:false,
      //线下支付方式
      payMethodList:[],
      //房产集合
      sfContractList:[],
      //付款状态
      payStateList:[],
      //账单类型
      billTypeList:[],
      //tabs标签
      activeName: 'first',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
     // multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        itemflag:null,
        pageNum: 1,
        pageSize: 10,
        payOrderNo: null,
        businessId: null,
        contractId: null,
        contractNo: null,
        contractType: null,
        tTerminationId: null,
        premiseId: null,
        premiseName: null,
        ownerId: null,
        ownerName: null,
        tenantId: null,
        tenantName: null,
        payType: null,
        payState: null,
        billType: null,
        itemType: null,
        otherItemName: null,
        paymentAmount: null,
        realPaymentAmount: null,
        withPayPlanId: null,
        paymentDaysBegin: null,
        paymentDaysEnd: null,
        payableDate: null,
        paidTime: null,
        payNumber: null,
        payMethod: null,
        otherPayMethod: null,
        payFlowNo: null,
        rentalPrice: null,
        tax: null,
        operationUser: null,
        totalPayNumber: null,
        repayType: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        payOrderNo: [
          { required: true, message: "付款单号不能为空", trigger: "blur" }
        ],
        businessId: [
          { required: true, message: "商户ID不能为空", trigger: "blur" }
        ],
        contractId: [
          { required: true, message: "合同ID不能为空", trigger: "blur" }
        ],
        contractType: [
          { required: true, message: "合同类型1：收房合同，2：出房合同不能为空", trigger: "change" }
        ],
        delFlg: [
          { required: true, message: "删除标识,0:未删除，1：删除不能为空", trigger: "blur" }
        ],
        createUserid: [
          { required: true, message: "创建用户ID不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateUserid: [
          { required: true, message: "更新用户ID不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDict();
  },
  methods: {
    getDict() {
      dictInfo().then(res=>{
        console.log("========",res.data.sfContractList)
        this.payStateList=res.data.payStateList.data
        this.billTypeList=res.data.billTypeList.data
        this.payMethodList=res.data.payMethodList.data
      })
    },
    choosePayMethod(row) {
      this.dialogFormVisible=true;
      console.log("支付点击事件.......",row)
      let flag=true;

      this.payForm.paymentAmount=row.paymentAmount;
      this.payForm.id=row.paymentId
    },
    //支付
    payMoney() {
      let flag=true;
      if (this.payForm.payMethod == null || this.payForm.payMethod == '') {
          alert("信息不可为空");
          flag=false;
      }
      if (flag) {
        payInfo(this.payForm).then(res=>{
            alert("支付成功");
            this.dialogFormVisible=false;
            this.getList();
        })
      }
    },
    //tabs标签的点击事件
    handleClick(tab, event) {
      console.log(tab, event);
      this.activeName=tab.name
      this.getList();
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      if (this.activeName == "first") {
        this.queryParams.itemflag=1;
      }else if (this.activeName == "second") {
        this.queryParams.itemflag=2;
      }else if (this.activeName == "third") {
        this.queryParams.itemflag=3;
      }
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        payOrderNo: null,
        businessId: null,
        contractId: null,
        contractNo: null,
        contractType: null,
        tTerminationId: null,
        premiseId: null,
        premiseName: null,
        ownerId: null,
        ownerName: null,
        tenantId: null,
        tenantName: null,
        payType: null,
        payState: null,
        billType: null,
        itemType: null,
        otherItemName: null,
        paymentAmount: null,
        realPaymentAmount: null,
        withPayPlanId: null,
        paymentDaysBegin: null,
        paymentDaysEnd: null,
        payableDate: null,
        paidTime: null,
        payNumber: null,
        payMethod: null,
        otherPayMethod: null,
        payFlowNo: null,
        rentalPrice: null,
        tax: null,
        operationUser: null,
        totalPayNumber: null,
        repayType: null,
        remark: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      //this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPlan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePlan(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlan(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPlan(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('finance/paymentplan/export', {
        ...this.queryParams
      }, `system_plan.xlsx`)
    }
  }
};
</script>
